package fast;

import java.util.*;

/**
 * Statemanager class
 * 
 * Maintains an arraylist of arrays to help with message state management.
 * 
 * 
 * @author Adam Bell
 * @version 0.1
 * @date 22/12/2009
 */
public class StateManager {

	// arraylist of 2d String arrays. String[0] is the message ID, String[1] is
	// the IP address
	private ArrayList<String[]> state = new ArrayList<String[]>();

	/**
	 * Add an entry to the arraylist
	 * 
	 * @param id
	 *            Message ID
	 * @param ip
	 *            IP address
	 */
	public void add(int id, String ip) {

		String[] pushArray = { String.valueOf(id), ip };

		state.add(pushArray);
	}// add

	/**
	 * Check that the state arraylist contains at least one array with a first
	 * entry set as id
	 * 
	 * @param id
	 *            The message ID to search for
	 * @return true if a match found, false otherwise
	 */
	public boolean contains(int id) {

		boolean found = false;
		String[] check_array;
		Iterator<String[]> i = state.iterator();

		while (i.hasNext()) {
			check_array = (String[]) i.next();

			if (check_array[0].equals(String.valueOf(id))) {
				found = true;
				break;
			}
		}

		return found;
	}// contains()

	/**
	 * Check that the state arraylist contains at least one array with a second
	 * entry set as ip
	 * 
	 * @param ip
	 *            The IP address to search for
	 * @return true if a match found, false otherwise
	 */
	public boolean contains(String ip) {

		boolean found = false;
		String[] check_array;
		Iterator<String[]> i = state.iterator();

		while (i.hasNext()) {
			check_array = (String[]) i.next();

			if (check_array[1].equals(ip)) {
				found = true;
				break;
			}
		}

		return found;
	}// contains()

	/**
	 * Check that the state arraylist contains at least one array with a first
	 * entry set as id and the second entry set as ip
	 * 
	 * @param ip
	 *            The IP address to search for
	 * @param id
	 *            The message ID to search for
	 * @return true if a match found, false otherwise
	 */
	public boolean contains(int id, String ip) {

		boolean found = false;
		String[] check_array;
		Iterator<String[]> i = state.iterator();

		while (i.hasNext()) {
			check_array = (String[]) i.next();

			if (check_array[0].equals(String.valueOf(id))
					&& check_array[1].equals(ip)) {
				found = true;
				break;
			}
		}

		return found;
	}// contains()

	/**
	 * Remove all entries that match the provided ID
	 * 
	 * @param id
	 */
	public void remove(int id) {
		String[] check_array;

		Iterator<String[]> i = state.iterator();

		while (i.hasNext()) {
			check_array = (String[]) i.next();

			if (check_array[0].equals(String.valueOf(id))) {
				i.remove();
			}
		}

	}// remove()

	/**
	 * Remove all entries for a particular IP
	 * 
	 * @param ip
	 */
	public void remove(String ip) {
		String[] check_array;

		Iterator<String[]> i = state.iterator();

		while (i.hasNext()) {
			check_array = (String[]) i.next();

			if (check_array[1].equals(ip)) {
				i.remove();
			}
		}
	}// remove()

	/**
	 * Remove all entries that match both IP and ID
	 * 
	 * @param id
	 * @param ip
	 */
	public void remove(int id, String ip) {
		String[] check_array;

		Iterator<String[]> i = state.iterator();

		while (i.hasNext()) {
			check_array = (String[]) i.next();

			if (check_array[0].equals(String.valueOf(id))
					&& check_array[1].equals(ip)) {
				i.remove();
			}
		}
	}// remove()
	
	/**
	 * Print all entries currently in the statemanager
	 */
	public void printAll(){
		Iterator<String[]> stateIter = state.iterator();
		String[] stateArray;
		
		System.out.println("==========================StateManager========================");
		
		while(stateIter.hasNext()){
			stateArray = stateIter.next();
			
			for(int i = 0; i< stateArray.length; i++){
				System.out.println(stateArray[i]);
			}
		}
	}
}
